define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'duizhang/index' + location.search,
                    add_url: 'duizhang/add',
                    edit_url: 'duizhang/edit',
                    del_url: 'duizhang/del',
                    multi_url: 'duizhang/multi',
                    import_url: 'duizhang/import',
                    table: 'duizhang',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('选择运单'),
                                    title: __('选择运单'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'duizhang/xuanzhe',
                                    visible: function (row) {
                                        if (row.tijiaotime > 86400) return false;
                                        return true;
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: __('提交客户确认'),
                                    title: __('通知客户确认对账单'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'duizhang/tijiao',
                                    confirm: '确认提交客户确认',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg  );
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }],
                            formatter: Table.api.formatter.operate},
                        {field: 'id', title: __('Id')},
                        {field: 'fayun_id', title: __('Fayun_id')},
                        {field: 'bianhao', title: __('编号')},
                        {field: 'jine', title: __('Jine'), operate:'BETWEEN'},
                        {field: 'tijiaotime', title: __('Tijiaotime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'querentime', title: __('Querentime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        xuanzhe: function () {

            Table.api.init();
            //选择运单

            //---------------------------------------------------------------------------------------------------------------------------
            //保险信息
            var table_baoxian = $("#yundantable");
            table_baoxian.bootstrapTable({
                url: 'yundan/duizhanglist/' + location.search,
                extend: {
                    index_url: 'yundan/duizhanglist' + location.search,
                    multi_url: '',
                    table: 'yundan',
                },
                toolbar: '#tb_yundan',
                sortName: 'id', showColumns: false, showToggle: false, showExport: false, showSearch: false, commonSearch: false, search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'admin_id', title: __('Admin_id'),visible:false},
                        {field: 'id', title: __('id'), operate: 'LIKE'},
                        {field: 'chepai', title: __('Chepai'), operate: 'LIKE'},
                        {field: 'checi', title: __('Checi'), operate: 'LIKE'},
                        {field: 'start', title: __('Start'), operate: 'LIKE'},
                        {field: 'desti', title: __('Desti'), operate: 'LIKE'},
                        {field: 'huowu', title: __('Huowu'), operate: 'LIKE'},
                        {field: 'yunfei', title: __('运费'), operate: 'LIKE'},


                    ]
                ]
            });
           // table_baoxian.on('load-success.bs.table', function (e, data) {console.log(data);$("#baoxianmoney").text(data.extend.money);});
            Table.api.bindevent(table_baoxian);


       /*     //获取选中项
            $(document).on("click", ".btn-selected", function () {
                //在templateView的模式下不能调用table.bootstrapTable('getSelections')来获取选中的ID,只能通过下面的Table.api.selectedids来获取
                Layer.alert(JSON.stringify(Table.api.selectedids(table_baoxian)));
            });*/

            // 启动和暂停按钮
            $(document).on("click", ".btn-start,.btn-pause", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table_baoxian);
                Table.api.multi("btnstart", ids.join(","), table_baoxian, this);
            });

      /*      // 启动和暂停按钮
            $(document).on("click", ".btn-selected", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table_baoxian);

                Layer.confirm(
                    __("确定%s条记录加入结算单?",ids.length),
                    {icon:3,title:__('Warning'),offset:0,shadeClose:true},
                    function (index) {
                        Table.api.multi("changestatus", ids.join(","), table_baoxian, this);
                      //  $(".fa-refresh").trigger('click');
                        Layer.close(index)
                    }
                );


            });*/



            Controller.api.bindevent();
        },add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});